#Causal Impact Test
# Loading  necessary libraries
library(CausalImpact)
library(Synth)
library(dplyr)
library(writexl)
library(tidyr)
library(ggplot2)
library(zoo)

# Loading panel data
data <- read.csv("base_4dic.csv", row.names = 1) #GDP Per Capita Constant 2015 - WORLD BANK
data <- data %>% subset(year <= 2009)
head(data)
# Convert GDP column to numeric (replace commas with dots)
data$gdp <- as.numeric(gsub(",", ".", data$gdp))

# Pivot the data
reshaped_data <- pivot_wider(data, 
                             id_cols = year, 
                             names_from = country_name, 
                             values_from = gdp)

# Reorder columns to have Chile first
reshaped_data <- reshaped_data[, c("year", "Chile", setdiff(names(reshaped_data), "year"))]

# Display the first few rows of reshaped data
data<- reshaped_data
data <- data[, -1]
data <- data[, -7]
data <- data[, !colnames(data) %in% c('China', 'Philippines', 'Canada', 'United States', 'South Africa','Australia')]
#head(data)

pre.period <- c(1, 14) #Intervention 1990
post.period <- c(15, 34)

impact <- CausalImpact(data, pre.period, post.period, alpha = 0.1, model.args = list(niter = 10000)) #90% Confidence Interval
plot(impact)

